import { app, BrowserWindow, ipcMain } from 'electron';
import * as path from 'path';
import * as remote from '@electron/remote/main';
import { getConfig, setConfig, getAllConfig, clearConfig } from './store';

// 初始化remote模块
remote.initialize();

function createWindow() {
  const mainWindow = new BrowserWindow({
    width: 800,
    height: 600,
    webPreferences: {
      nodeIntegration: true,
      contextIsolation: false,
      sandbox: false
    }
  });

  // 为窗口启用remote模块
  remote.enable(mainWindow.webContents);

  mainWindow.loadFile(path.join(__dirname, '../renderer/index.html'));
  
  // 开发时打开开发者工具
  mainWindow.webContents.openDevTools();
}

// 设置 IPC 通信处理器
ipcMain.handle('get-config', (event, key) => {
  return getConfig(key);
});

ipcMain.handle('set-config', (event, key, value) => {
  setConfig(key, value);
  return true;
});

ipcMain.handle('get-all-config', () => {
  return getAllConfig();
});

ipcMain.handle('clear-config', () => {
  clearConfig();
  return true;
});

app.whenReady().then(() => {
  createWindow();

  app.on('activate', function () {
    if (BrowserWindow.getAllWindows().length === 0) createWindow();
  });
});

app.on('window-all-closed', function () {
  if (process.platform !== 'darwin') app.quit();
}); 